package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 根据分类id查询对应的分类对象，返回分类的dto
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public ResponseEntity<CategoryDTO> findCategoryById(@PathVariable("id") Long id){
        //return ResponseEntity.ok(this.categoryService.getById(id));  返回的实体类不对
        //return ResponseEntity.ok(new CategoryDTO(this.categoryService.getById(id)));  返回类型对，单不应该有方法
        return ResponseEntity.ok(this.categoryService.findCategoryById(id));
    }


    /**
     * 根据分类的id集合，查询对应的分类集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> listCategoryByIds(
            @RequestParam("ids")List<Long> ids){

        return ResponseEntity.ok(this.categoryService.listCategoryByIds(ids));
    }



    @GetMapping("/of/parent")
    public ResponseEntity <List<CategoryDTO>> listCategoryByParent(@RequestParam("pid")Long pid){

         return ResponseEntity.ok(this.categoryService.listCategoryByParent(pid));

    }

    @GetMapping("/of/brand")
    public ResponseEntity <List <CategoryDTO> > listCategoryByBrand(@RequestParam("id") Long bid){
        return ResponseEntity.ok(this.categoryService.listCategoryByBrand(bid));
    }
}
